<template>
  <div class="wh">
    <Earth
      :showToolBox="false"
      :showEagleView="false"
      :showStatusBar="false"
      @ready="earthReady"
      :mode2d="false"
    />
  </div>
</template>
<script setup>
import { ref, onMounted, reactive, defineProps } from "vue";
import { apiList } from "@/api/apiList";

let props = defineProps({
  itemOptions: Object,
});

function wapian() {
  earth.sceneTree.$refs.test_img.czmObject.alpha = 0.44;
  // 判断4326还是3857
  // 默认3857
  // 4326
  if (props.itemOptions.projectionMode == "3857") {
    let finalurl = `/api/mapServer/tile/${props.itemOptions.analysePath}/{z}/{x}/{y}`;
    viewer.imageryLayers.addImageryProvider(
      new Cesium.UrlTemplateImageryProvider({
        url: finalurl,
        // rectangle: rect,
        //minimumLevel:0,
        //maximumLevel:18
      })
    );
  } else {
    console.log(11111);
    let finalurl = `/api/mapServer/tile/${props.itemOptions.analysePath}/{z}/{x}/{y}`;
    viewer.imageryLayers.addImageryProvider(
      new Cesium.UrlTemplateImageryProvider({
        url: finalurl,
        // tileHeight: 256,
        // tileWidth: 256,
        customTags: {
          oz(imageProvider, x, y, level) {
            return level + 1;
          },
        },
        tilingScheme: new Cesium.GeographicTilingScheme({}),
        // rectangle: rect,
        //minimumLevel:0,
        //maximumLevel:18
      })
    );
  }
}

function xiangqianxian(params) {
  apiList.wapian.getLineData(props.itemOptions.batchId).then((res) => {
    res.data.map((item) => {
      let geojson = JSON.parse(item.lineData);
      Cesium.GeoJsonDataSource.load(geojson, {
        stroke: Cesium.Color.HOTPINK,
        fill: Cesium.Color.fromAlpha(Cesium.Color.RED, 0.3),
        strokeWidth: 3,
      }).then((res) => {
        viewer.dataSources.add(res);
        viewer.flyTo(res);
      });
    });
  });
}

function xiangqianxianid() {
  // 添加镶嵌线
  apiList.getLineLiID(props.itemOptions.lineId).then((res) => {
    let geojson = JSON.parse(res.data.lineData);
    console.log(geojson);
    Cesium.GeoJsonDataSource.load(geojson, {
      stroke: Cesium.Color.HOTPINK,
      fill: Cesium.Color.fromAlpha(Cesium.Color.RED, 0.3),
      strokeWidth: 3,
    }).then((res) => {
      viewer.dataSources.add(res);
      viewer.flyTo(res);
    });
  });

  // let finalurl = `/api/mapServer/tile/${props.itemOptions.batchVersion}/{z}/{x}/{y}`;
  let finalurl = `/mapServer/tileBySearch/${props.itemOptions.lineVersion}/${props.itemOptions.captureDate}/{z}/{x}/{y}`;
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: finalurl,
      // rectangle: rect,
      //minimumLevel:0,
      //maximumLevel:18
    })
  );
}

function earthReady(params) {
  if (props.itemOptions.type == "showtiles") {
    wapian();
  }
  if (props.itemOptions.type == "xiangqianxian") {
    xiangqianxian();
  }
  if (props.itemOptions.type == "xiangqianxianlineID") {
    xiangqianxianid();
  }
}
</script>
<style lang="scss" scoped></style>
